<div class="box">
    <div class="heading">
      <h1><img src="<?php echo Yii::app()->request->baseUrl; ?>/images/book.png" alt="" />Management Source</h1>
      <div class="buttons">
          <?php echo CHtml::link('Generate news',array('source/gennews'),array('class'=>'button','id'=>'gen')); ?>
          <?php echo CHtml::link('Insert',array('source/create'),array('class'=>'button')); ?>
          <a onclick="confirm('Are you sure you want to delete this book?')" class="button">Delete</a>
      </div>
    </div>
    <div class="content">
      <form action="delete" method="post" enctype="multipart/form-data" id="form">
        <table class="list">
          <thead>
            <tr>
              <td width="1" style="text-align: center;"><input type="checkbox" onclick="$('input[name*=\'selected\']').attr('checked', this.checked);"></td>
              <td class="left">Source ID</td>
              <td class="left">Source Name</td>
              <td class="left">Source Seletor</td>
              <td class="left">Created At</td>
              <td class="left">Updated At</td>
              <td class="center">Action</td>
            </tr>
          </thead>
          <tbody>
            <tr class="search">
              <td></td>
              
              <td><input type="text" name="search-source-id" value=""></td>
              <td><input type="text" name="search-source-name" value=""></td>
              <td><input type="text" name="search-source-selector" value=""></td>
              <td><input type="text" name="search-source-created-at" value=""></td>
              <td><input type="text" name="search-source-updated-at" value=""></td>
              <td align="center"><a id="search-button" class="button">Search</a></td>
              
            </tr>
            <?php foreach($sources as $source){?>
            <tr>
              <td style="text-align: center;">
                <input type="checkbox" name="selected[]" value="book_id" />
              </td>
              <td class="left"><?php echo $source->source_id ?></td>
              <td class="left"><?php echo $source->source_name?></td>
              <td class="left"><?php echo $source->source_selector?></td>
              <td class="left"><?php echo date("d-m-Y h:i:s",strtotime($source->source_created_at))?></td>
              <td class="left"><?php echo date("d-m-Y h:i:s",strtotime($source->source_updated_at))?></td>
              <td class="center">
                  <?php echo CHtml::link('View', array('source/view', 'id'=> $source->source_id)); ?>|
                  <?php echo CHtml::link('Update', array('source/update', 'id'=> $source->source_id)); ?>|
                  <?php echo CHtml::link('Delete',"#", array("submit"=>array('delete', 'id'=>$source->source_id), 'confirm' => 'Are you sure?')); ?>
              </td>
            </tr>
            <?php }?>
          </tbody>
        </table>
      </form>
    </div>
  </div>
  <script>
  function search(){
      //check and display all elements after searching
       $('table tbody tr').css('display','');
      //end check
      var source_id = $('input[name="search-source-id"]').val();
      var source_name = $('input[name="search-source-name"]').val();
      var source_selector = $('input[name="search-source-selector"]').val();
      if( source_id != ""){
      var ids = $('table tbody tr td:nth-child(2)');
      for(var i=1;i<ids.length;i++){
        value = $('table tbody tr td:nth-child(2)')[i].firstChild.data;
        if(value.indexOf(source_id)==-1) $('table tbody tr td:nth-child(2)')[i].parentNode.style.display='none'
      }  
      }
      if( source_name!=""){
        var names = $('table tbody tr td:nth-child(3)');
        for(var i=1;i<names.length;i++){
          value = $('table tbody tr td:nth-child(3)')[i].firstChild.data;
          if(value.indexOf(source_name)==-1) $('table tbody tr td:nth-child(3)')[i].parentNode.style.display='none'
        }
      
      }
      if(source_selector!=""){
        var selectors = $('table tbody tr td:nth-child(4)');
        for(var i=1;i<selectors.length;i++){
          value = $('table tbody tr td:nth-child(4)')[i].firstChild.data;
          if(value.indexOf(source_selector)==-1) $('table tbody tr td:nth-child(4)')[i].parentNode.style.display='none'
        }
      
      }
      
    }
  $(document).ready(function(){
    $('#search-button').click(search);
    $('table tbody tr:nth-child(1) input').bind('keypress',function(e){
      var code = (e.keyCode ? e.keyCode : e.which);
       if(code == 13) { //Enter keycode
         search();
       }
    });
    $(":checkbox").click(function(){
      var checked = $("input:checked");
      if(checked.length >0) {
      if(checked.length>1) alert('check 1 cai thoi ban tre');
      var id = checked[0].parentNode.nextSibling.nextSibling.firstChild.data;
      if(id!="Source ID")
      $("#gen").attr("href","gennews/"+id); 
    }
      
    });
    
  });
  </script>